package com.newtype.meeting.client.widget.frontend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.grid.RowExpander;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Element;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Meeting;
import com.newtype.meeting.client.model.MeetingRecord;
import com.newtype.meeting.client.model.MeetingReport;
import com.newtype.meeting.shared.MeetingNotifyDTO;

/**
 * 会议内容面板
 * @author 汪恭泽
 *
 */
public class MeetingContentPanel extends ContentPanel {
	private ListStore<MeetingRecord> recordStore = new ListStore<MeetingRecord>();
	private ListStore<MeetingReport> reportStore = new ListStore<MeetingReport>();
	private GroupingStore<MeetingNotifyDTO> notifyStore = new GroupingStore<MeetingNotifyDTO>();
	private DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
	private VerticalPanel westPanel;
	private ContentPanel centerPanel;

	public MeetingContentPanel(Meeting meeting) {
		setHeading("会议内容");
		setButtonAlign(HorizontalAlignment.LEFT);
		setLayout(new BorderLayout());

		createEastPanel();
		createCenterPanel(meeting);

		//判断会议是否取消
		if (meeting.getCancelTAG() != null && meeting.getCancelTAG() == 1) {
			addButton(getHeldRelatedButton());
		} else {
			addButton(getEditContentButton(meeting));
			addButton(getCancelMeetingButton(meeting));
			addButton(getReportButton(meeting));
			addButton(getRecordButton(meeting));
			addButton(getEditOpentTypeButton(meeting));
			addButton(getHeldRelatedButton());
			addButton(getNamedTableButton(meeting));
		}
	}

	//会议内容和会议记录
	public void createCenterPanel(Meeting meeting) {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER,
				600);
		centerData.setMargins(new Margins(5));

		westPanel = new VerticalPanel();
		westPanel.setScrollMode(Scroll.AUTO);
		westPanel.setStyleAttribute("background", "white");
		westPanel.setStyleAttribute("padding", "10px");
		westPanel.setLayout(new FlowLayout());

		String cancelTag = "";
		if (meeting.getCancelTAG() != null && meeting.getCancelTAG() == 1) {
			cancelTag = "<span class='span-tag'>已取消</span>";
		}

		StringBuffer sb1 = new StringBuffer();
		sb1.append("<span style='font-size: 15px;color: #369;'>"
				+ meeting.getCategoryName() + "</span><br/>");
		sb1.append("<div class='subject'>");
		sb1.append("<span class='span-title'>[ " + meeting.getSubject()
				+ " ]</span>&nbsp;&nbsp;");
		sb1.append(cancelTag);
		sb1.append("</div>");
		Text subject = new Text(sb1.toString());
		westPanel.add(subject);

		Text originator = new Text("<b>发起人：</b>" + meeting.getOriginatorName());
		originator.setStyleName("content-text");
		westPanel.add(originator);

		Text date = new Text("<br/><b>日期：</b>"
				+ dtf.format(meeting.getStartDate()) + "&nbsp;&nbsp;&nbsp;&nbsp;"
				+ meeting.getStartTime() + "~" + meeting.getEndTime());
		date.setStyleName("content-text");
		westPanel.add(date);

		if (meeting.getMeetingNO() != null) {
			Text meetingNo = new Text("<br/><b>编号：</b>"
					+ meeting.getMeetingNO());
			meetingNo.setStyleName("content-text");
			westPanel.add(meetingNo);
		}

		Text title = new Text("<br/><b>议题：</b>" + meeting.getSubject());
		title.setStyleName("content-text");
		westPanel.add(title);

		Text content = new Text("<br/><b>议程：</b><br/>" + meeting.getContent());
		content.setStyleName("content-text");
		westPanel.add(content);

		Text chair = new Text("<br/><b>会议主席：</b>" + meeting.getChairName());
		chair.setStyleName("content-text");
		westPanel.add(chair);

		Text recorder = new Text("<br/><b>会议记录：</b>"
				+ meeting.getRecorderName());
		recorder.setStyleName("content-text");
		westPanel.add(recorder);

		Text invited = new Text("<br/><b>与会人员：</b>" + meeting.getInvitedName());
		invited.setStyleName("content-text");
		westPanel.add(invited);

		if (meeting.getInvitedOthersName() != null) {
			Text invitedOther = new Text("<br/><b>列席人员：</b>"
					+ meeting.getInvitedOthersName());
			invitedOther.setStyleName("content-text");
			westPanel.add(invitedOther);
		}

		if (meeting.getLocation() != null) {
			Text location = new Text("<br/><b>会议地点：</b>"
					+ meeting.getLocation());
			location.setStyleName("content-text");
			westPanel.add(location);
		}

		westPanel.add(getRecordPanel());

		add(westPanel, centerData);
	}

	private ContentPanel getRecordPanel() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		XTemplate tpl = XTemplate.create("<p><b>记录内容:</b>{content}</p>");
		RowExpander expander = new RowExpander();
		expander.setTemplate(tpl);
		configs.add(expander);

		ColumnConfig column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("内容提示");
		column.setWidth(300);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("fullName");
		column.setHeader("记录人");
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig("submitDATE", "记录日期", 100);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		configs.add(column);

		ColumnModel cm = new ColumnModel(configs);

		ContentPanel cp = new ContentPanel();
		cp.setFrame(true);
		cp.setHeading("[ 本次会议记录 ]");
		cp.setLayout(new FitLayout());
		cp.setStyleAttribute("margin-top", "15px");
		cp.getHeader().addTool(new ToolButton("x-tool-refresh"));

		Grid<MeetingRecord> grid = new Grid<MeetingRecord>(recordStore, cm);
		grid.setBorders(false);
		grid.addPlugin(expander);
		grid.setAutoExpandColumn("subject");
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);
		grid.setSize(800, 200);

		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<MeetingRecord>>() {
					public void handleEvent(
							SelectionChangedEvent<MeetingRecord> be) {
						if (be.getSelection().size() > 0) {
							MeetingRecord record = be.getSelection().get(0);
							Dispatcher.forwardEvent(AppEvents.ToRecordWindow,
									record);
						}
					}
				});

		cp.add(grid);

		return cp;
	}

	private void createEastPanel() {
		centerPanel = new ContentPanel();
		centerPanel.setBodyBorder(false);
		centerPanel.setHeaderVisible(false);
		centerPanel.setStyleAttribute("background", "white");
		centerPanel.setLayout(new FlowLayout());
		BorderLayoutData east = new BorderLayoutData(LayoutRegion.EAST, 400);
		east.setMargins(new Margins(5));

		centerPanel.add(getReportPanel());
		centerPanel.add(getAttendPanel());

		add(centerPanel, east);
	}

	private ContentPanel getAttendPanel() {
		ContentPanel cp = new ContentPanel();
		cp.setHeading("[ 预订出席人员 ]");
		cp.setAnimCollapse(false);
		cp.setCollapsible(true);
		cp.setLayout(new FitLayout());
		cp.setAutoWidth(true);
		cp.setHeight(200);

		notifyStore.groupBy("deptName");
		ColumnConfig fullName = new ColumnConfig("fullName", "姓名", 200);
		ColumnConfig deptName = new ColumnConfig("deptName", "部门", 100);

		List<ColumnConfig> config = new ArrayList<ColumnConfig>();
		config.add(fullName);
		config.add(deptName);

		final ColumnModel cm = new ColumnModel(config);

		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setForceFit(true);
		view.setGroupRenderer(new GridGroupRenderer() {
			public String render(GroupColumnData data) {
				String l = data.models.size() == 1 ? "Item" : "Items";
				return data.group + " (" + data.models.size() + " " + l + ")";
			}
		});

		Grid<MeetingNotifyDTO> grid = new Grid<MeetingNotifyDTO>(notifyStore,
				cm);
		grid.setView(view);
		grid.setBorders(false);
		cp.add(grid);
		return cp;
	}

	private ContentPanel getReportPanel() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		XTemplate tpl = XTemplate
				.create("<p><b>报告内容:</b><span>{content}</span></p><br><p><b>登录时间:</b> {submitDate:date(\"yyyy-MM-dd HH:mm:ss\")}</p>");

		RowExpander expander = new RowExpander();
		expander.setTemplate(tpl);

		configs.add(expander);

		ColumnConfig column = new ColumnConfig();
		column.setId("fullName");
		column.setHeader("报告人");
		column.setWidth(100);
		configs.add(column);

		column = new ColumnConfig();
		column.setId("subject");
		column.setHeader("主题");
		column.setWidth(200);
		configs.add(column);

		ColumnModel cm = new ColumnModel(configs);

		ContentPanel cp = new ContentPanel();
		cp.setHeading("[ 本次会议报告 ]");
		cp.setAnimCollapse(false);
		cp.setCollapsible(true);
		cp.setLayout(new FitLayout());
		cp.setAutoWidth(true);
		cp.setHeight(200);

		Grid<MeetingReport> grid = new Grid<MeetingReport>(reportStore, cm);
		grid.addPlugin(expander);
		grid.setStripeRows(true);
		grid.setColumnLines(true);
		grid.setColumnReordering(true);
		grid.getView().setAutoFill(true);

		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<MeetingReport>>() {
					public void handleEvent(
							SelectionChangedEvent<MeetingReport> be) {
						if (be.getSelection().size() > 0) {
							MeetingReport report = be.getSelection().get(0);
							Dispatcher.forwardEvent(AppEvents.ToReportWindow,
									report);
						}
					}
				});
		cp.add(grid);

		return cp;
	}

	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);

	}

	private Button getEditContentButton(final Meeting meeting) {
		return new Button("修改会议内容", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher
						.forwardEvent(AppEvents.ToEditMeetingContent, meeting);
			}
		});
	}

	private Button getCancelMeetingButton(final Meeting meeting) {
		final Listener<MessageBoxEvent> listener = new Listener<MessageBoxEvent>() {
			public void handleEvent(MessageBoxEvent ce) {
				Button btn = ce.getButtonClicked();
				if (btn.getText().equals("Yes")) {
					Dispatcher.forwardEvent(AppEvents.CancelMeeting, meeting);
				}
			}
		};
		return new Button("取消本次会议", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				MessageBox.confirm("提示消息", "取消会议，系统会自动取消会议通知，是否继续？", listener);
			}
		});
	}

	private Button getReportButton(final Meeting meeting) {
		return new Button("登录会议报告", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//实例化一个会议报告对象
				MeetingReport report = new MeetingReport();
				report.setMeetingID(meeting.getId());
				Dispatcher.forwardEvent(AppEvents.ToReportWindow, report);
			}
		});
	}

	private Button getRecordButton(final Meeting meeting) {
		return new Button("维护会议记录", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//实例化一个会议记录对象
				MeetingRecord record = new MeetingRecord();
				record.setMeetingID(meeting.getId());
				Dispatcher.forwardEvent(AppEvents.ToRecordWindow, record);
			}
		});
	}

	private Button getEditOpentTypeButton(final Meeting meeting) {
		return new Button("修改公开对象", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher
						.forwardEvent(AppEvents.ToEditOpenTypeWindow, meeting);
			}
		});
	}

	private Button getHeldRelatedButton() {
		return new Button("召开相关会议", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {

			}
		});
	}

	private Button getNamedTableButton(final Meeting meeting) {
		return new Button("点名表", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Dispatcher.forwardEvent(AppEvents.ToNamedTableWindow, meeting);
			}
		});
	}

	public ListStore<MeetingRecord> getRecordStore() {
		return recordStore;
	}

	public ListStore<MeetingReport> getReportStore() {
		return reportStore;
	}

	public GroupingStore<MeetingNotifyDTO> getNotifyStore() {
		return notifyStore;
	}
}
